﻿@{
    Layout = "~/Views/Shared/_Form.cshtml";
}
@using Raffles.OA.Data;
@using Raffles.OA.Utility;
<style>
    .font {
        font-size: 15px;
    }

    .table tr {
        text-align: center;
    }

    .table thead tr td {
        font-weight: bold;
    }

    .table > tbody > tr > td {
        padding: 2px !important;
    }

    .flex-container {
        display: flex;
        align-items: center;
    }

    .form-control {
        background-color: #FAFAD2;
        height:30px;
    }
</style>
<form id="form" class="form-horizontal m" role="form">
    <div class="form-group">
        <div class="col-sm-12">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th colspan="7" class="title text-center text-bold text-red font">团组培训费用参考</th>
                    </tr>
                    <tr>
                        <td>分项内容</td>
                        <td>单项内容</td>
                        <td width="200px">单项成本</td>
                        <td width="200px">单项报价</td>
                        <td width="200px">单项数量</td>
                        <td width="150px">成本小计</td>
                        <td width="150px">报价小计</td>
                    </tr>
                </thead>
                <tbody id="activity_body">
                    <tr name="公务分项">
                        <td rowspan="3">公务分项</td>
                        <td>邀函</td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="COST" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>封</span>
                            </div>
                        </td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="SALE" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>封</span>
                            </div>
                        </td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="QTY" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;封</span>
                            </div>
                        </td>
                        <td>
                            <span class="text-bold" name="COST_TOTAL_A"></span>
                        </td>
                        <td>
                            <span class="text-bold" name="SALE_TOTAL_A"></span>
                        </td>
                    </tr>
                    <tr name="公务分项" mark="0">
                        <td>公务</td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="COST" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>场</span>
                            </div>
                        </td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="SALE" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>场</span>
                            </div>
                        </td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="QTY" col="TIME" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;次</span>
                            </div>
                        </td>
                        <td>
                            <span class="text-bold" name="COST_TOTAL_A"></span>
                        </td>
                        <td>
                            <span class="text-bold" name="SALE_TOTAL_A"></span>
                        </td>
                    </tr>
                    <tr name="公务分项">
                        <td>翻译</td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="COST" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>场</span>
                            </div>
                        </td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="SALE" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>场</span>
                            </div>
                        </td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="QTY" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;次</span>
                            </div>
                        </td>
                        <td>
                            <span class="text-bold" name="COST_TOTAL_A"></span>
                        </td>
                        <td>
                            <span class="text-bold" name="SALE_TOTAL_A"></span>
                        </td>
                    </tr>
                    <tr name="课程分项" mark="0">
                        <td rowspan="8">课程分项</td>
                        <td>课酬</td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="COST" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>场</span>
                            </div>
                        </td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="SALE" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>场</span>
                            </div>
                        </td>
                        <td rowspan="4">
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="QTY" col="TIME" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;次</span>
                            </div>
                        </td>
                        <td>
                            <span class="text-bold" name="COST_TOTAL_B"></span>
                        </td>
                        <td>
                            <span class="text-bold" name="SALE_TOTAL_B"></span>
                        </td>
                    </tr>
                    <tr name="课程分项">
                        <td>教室</td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="COST" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>场</span>
                            </div>
                        </td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="SALE" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>场</span>
                            </div>
                        </td>
                        <td>
                            <span class="text-bold" name="COST_TOTAL_B"></span>
                        </td>
                        <td>
                            <span class="text-bold" name="SALE_TOTAL_B"></span>
                        </td>
                    </tr>
                    <tr name="课程分项">
                        <td>陪同</td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="COST" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>场</span>
                            </div>
                        </td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="SALE" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>场</span>
                            </div>
                        </td>
                        <td>
                            <span class="text-bold" name="COST_TOTAL_B"></span>
                        </td>
                        <td>
                            <span class="text-bold" name="SALE_TOTAL_B"></span>
                        </td>
                    </tr>
                    <tr name="课程分项" isRelatePerson>
                        <td>茶歇</td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="COST" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>次</span>
                            </div>
                        </td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="SALE" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>次</span>
                            </div>
                        </td>
                        <td>
                            <span class="text-bold" name="COST_TOTAL_B"></span>
                        </td>
                        <td>
                            <span class="text-bold" name="SALE_TOTAL_B"></span>
                        </td>
                    </tr>
                    <tr name="课程分项">
                        <td>讲义</td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="COST" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>人</span>
                            </div>
                        </td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="SALE" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>人</span>
                            </div>
                        </td>
                        <td rowspan="4">
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="QTY" col="PERSON" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;人</span>
                            </div>
                        </td>
                        <td>
                            <span class="text-bold" name="COST_TOTAL_B"></span>
                        </td>
                        <td>
                            <span class="text-bold" name="SALE_TOTAL_B"></span>
                        </td>
                    </tr>
                    <tr name="课程分项">
                        <td>证书</td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="COST" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>人</span>
                            </div>
                        </td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="SALE" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>人</span>
                            </div>
                        </td>
                        <td>
                            <span class="text-bold" name="COST_TOTAL_B"></span>
                        </td>
                        <td>
                            <span class="text-bold" name="SALE_TOTAL_B"></span>
                        </td>
                    </tr>
                    <tr name="课程分项">
                        <td>拍照</td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="COST" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>人</span>
                            </div>
                        </td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="SALE" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>人</span>
                            </div>
                        </td>
                        <td>
                            <span class="text-bold" name="COST_TOTAL_B"></span>
                        </td>
                        <td>
                            <span class="text-bold" name="SALE_TOTAL_B"></span>
                        </td>
                    </tr>
                    <tr name="课程分项">
                        <td>礼品</td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="COST" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>人</span>
                            </div>
                        </td>
                        <td>
                            <div class="col-sm-12 flex-container">
                                <input type="number" name="SALE" class="form-control text-center" oninput="inputEvent($(this))" />
                                <span>&nbsp;/</span>
                                <span>人</span>
                            </div>
                        </td>
                        <td>
                            <span class="text-bold" name="COST_TOTAL_B"></span>
                        </td>
                        <td>
                            <span class="text-bold" name="SALE_TOTAL_B"></span>
                        </td>
                    </tr> 
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="3" rowspan="4"></td>
                        <td>公务报价</td>
                        <td>
                            <span class="text-bold" name="SALE_SUM_A">0.00</span>
                        </td>
                        <td>培训销售</td>
                        <td>
                            <span class="text-bold" name="SALE_SUM_AB">0.00</span>
                        </td>
                    </tr>
                    <tr>
                        <td>课程报价</td>
                        <td>
                            <span class="text-bold" name="SALE_SUM_B">0.00</span>
                        </td>
                        <td>培训成本</td>
                        <td>
                            <span class="text-bold" name="COST_SUM_AB">0.00</span>
                        </td>
                    </tr>
                    <tr>
                        <td>日均成本</td>
                        <td>
                            <span class="text-bold" name="COST_DAY_AVERAGE_AB">0.00</span>
                        </td>
                        <td>培训利润</td>
                        <td>
                            <span class="text-bold" name="PROFIT_AB">0.00</span>
                        </td>
                    </tr>
                    <tr>
                        <td>日均报价</td>
                        <td>
                            <span class="text-bold" name="SALE_DAY_AVERAGE_AB">0.00</span>
                        </td>
                        <td>利润比率</td>
                        <td>
                            <span class="text-bold" name="PROFIT_RATE_AB">0.00%</span>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</form>
<script type="text/javascript">
    var activityId = frame.request("id");
    $(function () {
        renderData();
    })

    function renderData() {
        frame.ajax({
            url: '@Url.Content("/Sale/BaseInfo/SetBaseActivityDeatilForm")' + '?activityId=' + activityId,
            type: 'get',
            success: function (obj) {
                if (obj.success) {
                    if (obj.data.length > 0) {
                        $('#activity_body tr').each(function (index) {
                            $(this).find('input[name=COST]').val(obj.data[index].ItemCostPrice).trigger('input');
                            $(this).find('input[name=SALE]').val(obj.data[index].ItemSalePrice).trigger('input');
                            if ($(this).find('input[name=QTY]').length > 0) {
                                $(this).find('input[name=QTY]').val(obj.data[index].ItemQty).trigger('input');
                            }
                        })
                    }
                }
            }
        });
    }
    //计算小计
    function inputEvent($this) {
        var $tr = $this.parent().parent().parent();
        var cost = parseFloat($tr.find('input[name=COST]').val()) || 0;
        var sale = parseFloat($tr.find('input[name=SALE]').val()) || 0;
        var qty = parseFloat($tr.find('input[name=QTY]').val()) || 0;
        var personTotal = parseFloat($('input[col=PERSON]').val()) || 0;

        //将合并单元的数量值追加到合并列的tr上，便于后面获取
        if ($this.attr("name") == "QTY" && $this.parent().parent().is('[rowspan]')) {
            $tr.attr("QTY", $this.val());
            //获取当前元素之后的3个兄弟节点
            $tr.nextAll().slice(0, 3).find('td').each(function () {
                $(this).parent().attr("QTY", $this.val());
            })
        }
        //这里的if是只针对编辑rowspan行时联动关联的合并行的数据计算，关联合并行的编辑联动在else里面实现
        if ($this.parent().parent().is('[rowspan]')) {
            qty = $tr.attr("QTY") || 0;
            $tr.find('span[name^=COST_TOTAL_]').text(cost * qty);
            $tr.find('span[name^=SALE_TOTAL_]').text(sale * qty);
            $tr.nextAll().slice(0, 3).each(function () {
                //识别是否需要关联人数
                if ($(this).is('[isRelatePerson]')) {
                    $(this).find('span[name^=COST_TOTAL_]').text((parseFloat($(this).find('input[name=COST]').val()) || 0) * qty * personTotal);
                    $(this).find('span[name^=SALE_TOTAL_]').text((parseFloat($(this).find('input[name=SALE]').val()) || 0) * qty * personTotal);
                }
                else {
                    $(this).find('span[name^=COST_TOTAL_]').text((parseFloat($(this).find('input[name=COST]').val()) || 0) * qty);
                    $(this).find('span[name^=SALE_TOTAL_]').text((parseFloat($(this).find('input[name=SALE]').val()) || 0) * qty);
                }
            })
            //如果编辑的是人数的数量，需要反向更新关联的isRelatePerson的合计
            if (!!$this.attr('col') && $this.attr('col') === "PERSON") {
                $('#activity_body tr[isRelatePerson]').each(function () {
                    $(this).find('input[name=COST]').trigger('input');
                });
            }
        }
        else {
            qty = (parseFloat($tr.find('input[name=QTY]').val()) || parseFloat($tr.attr("QTY")) || 0);
            //识别是否需要关联人数
            if ($tr.is('[isRelatePerson]')) {
                $tr.find('span[name^=COST_TOTAL_]').text(cost * qty * personTotal);
                $tr.find('span[name^=SALE_TOTAL_]').text(sale * qty * personTotal);
            }
            else {
                $tr.find('span[name^=COST_TOTAL_]').text(cost * qty);
                $tr.find('span[name^=SALE_TOTAL_]').text(sale * qty);
            }
        }
        //公务报价-公务分项全部总计
        var SALE_SUM_A = $('span[name=SALE_TOTAL_A]').map(function () { return parseFloat($(this).text()) || 0 }).get().reduce(function (total, value) { return total + value }, 0);
        $('span[name=SALE_SUM_A]').text(SALE_SUM_A.toFixed(2));

        //课程报价-课程分项总计
        var SALE_SUM_B = $('span[name=SALE_TOTAL_B]').map(function () { return parseFloat($(this).text()) || 0 }).get().reduce(function (total, value) { return total + value }, 0);
        $('span[name=SALE_SUM_B]').text(SALE_SUM_B.toFixed(2));

        //日均成本-全部成本/(公务+课程数量）*2
        var COST_SUM_A = $('span[name=COST_TOTAL_A]').map(function () { return parseFloat($(this).text()) || 0 }).get().reduce(function (total, value) { return total + value }, 0);
        var COST_SUM_B = $('span[name=COST_TOTAL_B]').map(function () { return parseFloat($(this).text()) || 0 }).get().reduce(function (total, value) { return total + value }, 0);
        var times = $('input[col=TIME]').map(function () { return parseFloat($(this).val()) || 0 }).get().reduce(function (total, value) { return total + value }, 0);
        $('span[name=COST_DAY_AVERAGE_AB]').text(times > 0 ? parseFloat((COST_SUM_A + COST_SUM_B) * 2 / times).toFixed(2) : "0.00");

        //日均报价-全部报价/(公务+课程数量）*2
        $('span[name=SALE_DAY_AVERAGE_AB]').text(times > 0 ? parseFloat((SALE_SUM_A + SALE_SUM_B) * 2 / times).toFixed(2) : "0.00");

        //培训销售-公务报价+课程报价
        $('span[name=SALE_SUM_AB]').text((SALE_SUM_A + SALE_SUM_B).toFixed(2));

        //培训成本-公务成本+课程成本
        $('span[name=COST_SUM_AB]').text((COST_SUM_A + COST_SUM_B).toFixed(2));

        //培训利润
        var PROFIT_AB = (SALE_SUM_A + SALE_SUM_B) - (COST_SUM_A + COST_SUM_B);
        $('span[name=PROFIT_AB]').text(((SALE_SUM_A + SALE_SUM_B) - (COST_SUM_A + COST_SUM_B)).toFixed(2));

        //利润比率
        $('span[name=PROFIT_RATE_AB]').text(parseFloat(PROFIT_AB * 100 / (SALE_SUM_A + SALE_SUM_B) || 0).toFixed(2) + "%");
    }

    function saveForm(index) {
        var invalidInput = $('input[type=number]').filter(function () {
            return $(this).val() === "";
        });
        if (invalidInput.length == 0) {
            var activityDeatil = [];
            $('#activity_body tr').each(function () {
                var name = $(this).attr("name");//分项内容
                var itemName = $(this).find("td").eq(0).is('[rowspan]') ? $(this).find("td").eq(1).text() : $(this).find("td").eq(0).text();//单项内容
                var itemCostPrice = parseFloat($(this).find('input[name=COST]').val()) || 0;//单项成本
                var itemSalePrice = parseFloat($(this).find('input[name=SALE]').val()) || 0;//单项报价
                var itemQty = $(this).attr("QTY") ? (parseFloat($(this).attr("QTY")) || 0) : (parseFloat($(this).find('input[name=QTY]').val()) || 0);//单项数量
                var mark = ($(this).is('[mark]') && $(this).attr("mark") == 0) ? false : true;
                activityDeatil.push({
                    activityId, name, itemName, itemCostPrice, itemSalePrice, itemQty, mark
                })
            });
            var postData = { activityDeatil,activityId };
            frame.ajax({
                url: '@Url.Content("/Sale/BaseInfo/SaveBaseActivityDeatilForm")',
                type: 'post',
                data: postData,
                success: function (obj) {
                    if (obj.success) {
                        frame.msgSuccess(obj.message);
                        parent.PageUI.searchGrid("@QuotationItemEnum.GroupActivity.ToString()");
                        parent.layer.close(index);
                    }
                    else {
                        frame.msgError(obj.message);
                    }
                }
            });
        }
        else {
            frame.msgWarning("表单维护不完整！");
        }
    }
</script>